package app;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import app.structure.Graph;


public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File startFile = null;
		try {
			startFile = new File(args[0]);
			if(args.length == 2 && args[1].equals("scan")) {
				LocalSpider s = new LocalSpider(startFile);
				s.generateGraph();
				s.saveGraph();

				System.out.println(s.toString());
			}
			else if(args.length == 3 && args[0].equals("place")) {
				int howMany = -1;
				howMany = Integer.parseInt(args[1]);

				LocalSpider s = new LocalSpider(startFile);
				Graph g = s.loadGraph();
				if(g != null) {
					URL u = new URL(args[2]);
					System.out.println(s.toString()+"\n");
					DecoyPlacer dp = new DecoyPlacer(g);
					dp.placeDecoys(u.toString(), howMany);
					System.out.println("Done.");
				}
			}
			else if(args.length == 2 && args[0].equals("removeall")) {
				LocalSpider s = new LocalSpider(startFile);
				Graph g = s.loadGraph();
				if(g != null) {
					URL u = new URL(args[1]);
					System.out.println(s.toString()+"\n");
					DecoyPlacer dp = new DecoyPlacer(g);
					dp.removeDecoys(u.toString());
					System.out.println("Done.");
				}
			}
			else {
				System.out.println("args: D:\\Universita\\workspace\\Java\\sc\\WebContent\\index.html scan");
				System.out.println("args: place 100 http://192.168.2.108:8080/sc/articles/decoy.html");
				System.out.println("args: removeall http://192.168.2.108:8080/sc/articles/decoy.html");
			}
		} catch (MalformedURLException e) {
			System.out.println(startFile + " is not a valid path");
		} catch (NumberFormatException e) {
			System.out.println(args[1] + " is not a valid number");
		} catch (IOException e) {
			System.out.println("Couldn't access graph.ser; run again the program in scan mode");
		}
	}
}

